package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.servicebase.exceptionhandler.CustomizeException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hwt
 * @since 2022-08-23
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {


    @Resource
    EduVideoMapper videoMapper;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseID) {

        // 根据课程id，查询所有章节
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseID);
        List<EduChapter> chapters = baseMapper.selectList(wrapper);

        // 根据课程 id，查询到所有的小结
        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper.eq("course_id", courseID);
        List<EduVideo> videos = videoMapper.selectList(wrapper2);

        List<ChapterVo> list = new ArrayList<>();

        // 拼接章节小结结构
        this.DataCuration(list, chapters, videos);

        return list;

    }

    @Override
    public boolean deleteChapter(String chapterId) {
        // 先根据章节 id ，去查询小结表，结果不为 null 就不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        // 由于我们不需要数据，只需要知道有没有数据，所有可以使用 统计查询方法
        Integer integer = videoMapper.selectCount(wrapper);

        if (integer > 0) { // 大于 0 表示还有小结，就不进行删除
            throw new CustomizeException(20001, "请先删除小结，再删除章节");
        } else { // 没有查询到小结数据，进行删除
            return baseMapper.deleteById(chapterId) == 1;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }

    private void DataCuration(List<ChapterVo> list, List<EduChapter> chapters, List<EduVideo> videos) {

        // 遍历所有查询到的章节
        for (EduChapter chapter : chapters) {
            // 每遍历一个章节，都将数据存进 vo 中，并存放到 list 里面
            ChapterVo chapterVo = new ChapterVo(chapter.getId(), chapter.getTitle());
            list.add(chapterVo);

            // 遍历所有小结
            for (EduVideo video : videos) {
                // 判断小结所属章节 id，和当前章节是否一致
                if (chapter.getId().equals(video.getChapterId())) {

                    // 一致的话，就将数据封装到 vo 中去
                    VideoVo videoVo = new VideoVo(video.getId(), video.getTitle(),video.getVideoSourceId());

                    // 封装好的 vo 再存放到 chapterVo 中去
                    chapterVo.getChildren().add(videoVo);

                }
            }
        }

    }
}
